package org.yi.fc.utils;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * FIXME: 暂时通过这个方法将dao操作集中到实体类中操作， 以后通过反射对数据库进行映射
 * @author qq
 *
 */
public class ParamUtils {
	
	private static final Logger logger = LoggerFactory.getLogger(ParamUtils.class);

	public static Integer getInt(HttpServletRequest request, String key) {
		String obj = request.getParameter(key);
		if(!Pattern.matches("\\d*", obj)) {
			logger.error("无法获取Integer值， key：{}, value:{}", key, obj);
			return null;
		}
		return Integer.parseInt(obj);
	}
	
	public static Long getLong(HttpServletRequest request, String key) {
		String obj = request.getParameter(key);
		if(!Pattern.matches("\\d*", obj)) {
			logger.error("无法获取Long值， key：{}, value:{}", key, obj);
			return null;
		}
		return Long.parseLong(obj);
	}
	
	public static String get(HttpServletRequest request, String key) {
		return getString(request, key);
	}
	
	public static String getString(HttpServletRequest request, String key) {
		return request.getParameter(key);
	}
	
	public static Boolean getBoolean(HttpServletRequest request, String key) {
		String value = get(request, key);
		return "true".equalsIgnoreCase(value);
	}
	
}
